from mainfunc.baseapi import BaseApi
from config.fileName import title_dic_sz


class HandleFilesz(BaseApi):
    def __init__(self):
        self.url = "http://www.szse.cn/marketServices/technicalservice/interface/"
        self.req = BaseApi.getSource(self.url)

    def downF(self):
        for file_name in list(title_dic_sz.keys()):
            partten = f"var curHref = './(P\d+.pdf?)';[\n][\s]*//var curTitle = '{file_name}"
            try:
                self.download_name = BaseApi.searchName(partten, self.req)
                self.download_url = BaseApi.combineUrl(self.url, self.download_name)
                fileTpye = self.download_name.split('.')[-1]
                BaseApi.cleanFile(title_dic_sz[file_name])
                BaseApi.downFile(fileTpye, file_name, self.download_url)
                print(f'文件:{file_name}处理成功')
            except Exception as e:
                print(f'文件:{file_name}处理失败,错误信息：{e}')
